/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */

package com.library.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.library.common.base.ApiResult;
import com.library.entity.ResourceEntity;
import com.library.entity.UserResource;
import com.library.entity.dto.SearchDto;
import com.library.entity.vo.ArticleCatalogVo;
import com.library.service.ResourceService;
import com.library.service.UserResourceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 资源
 *
 * @author pig
 * @date 2023-10-31 14:34:33
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("ruralLibrary/resource")
@Api(tags = "资源管理", value = "资源管理")
public class ResourceController {

    @Resource
    private ResourceService resourceService;

    @Resource
    private UserResourceService userResourceService;

    /**
     * 分页查询
     *
     * @param page     分页对象
     * @param resource 资源
     * @return
     */
    @ApiOperation(value = "分页查询")
    @GetMapping("/page")
    public ApiResult getresourcePage(Page page, ResourceEntity resource) {
        LambdaQueryWrapper<ResourceEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.like(StrUtil.isNotBlank(resource.getResourceName()), ResourceEntity::getResourceName, resource.getResourceName());
        wrapper.like(StrUtil.isNotBlank(resource.getAuthor()), ResourceEntity::getAuthor, resource.getAuthor());
        wrapper.like(StrUtil.isNotBlank(resource.getPress()), ResourceEntity::getPress, resource.getPress());
        wrapper.eq(resource.getBookTypeId() != null, ResourceEntity::getBookTypeId, resource.getBookTypeId());
        wrapper.eq(resource.getDataTypeId() != null, ResourceEntity::getDataTypeId, resource.getDataTypeId());
        wrapper.ne(ResourceEntity::getResourceId, 0);
        return resourceService.pageList(page, wrapper);
    }


    /**
     * 通过id查询资源
     *
     * @param resourceId id
     * @return ApiResult
     */
    @ApiOperation(value = "通过id查询")
    @GetMapping("/{resourceId}")
    public ApiResult getById(@PathVariable("resourceId") Long resourceId) {
        return ApiResult.ok(resourceService.getById(resourceId));
    }

    /**
     * 新增资源
     *
     * @return ApiResult
     */
    @ApiOperation(value = "新增资源")
    @PostMapping("/add")
    public ApiResult save() {
        return resourceService.saveAll();
    }

    /**
     * 修改资源
     *
     * @param resource 资源
     * @return ApiResult
     */
    @ApiOperation(value = "修改资源")
    @PostMapping("/update")
    public ApiResult updateById(@RequestBody ResourceEntity resource) {
        return ApiResult.ok(resourceService.updateById(resource));
    }

    /**
     * 通过id删除资源
     *
     * @param ids resourceId列表
     * @return ApiResult
     */
    @ApiOperation(value = "通过id删除资源")
    @PostMapping("/delete")
    public ApiResult removeById(@RequestBody Long[] ids) {
        return ApiResult.ok(resourceService.removeByIds(CollUtil.toList(ids)));
    }


    /**
     * 导出excel 表格
     *
     * @param resource 查询条件
     * @return excel 文件流
     */
    @GetMapping("/export")
    public List<ResourceEntity> export(ResourceEntity resource) {
        return resourceService.list(Wrappers.query(resource));
    }

    @GetMapping("/pageView/{resourceId}")
    public void pageView(@PathVariable("resourceId") Long resourceId) {
        resourceService.pageView(resourceId);
    }

    @GetMapping(value = "/getProductResourcesDetails/{resourceId}")
    public ApiResult getProductResourcesDetails(@PathVariable("resourceId") String resourceId, HttpServletResponse response) {
        response.setHeader("Accept-Ranges","bytes");
        return resourceService.getProductResourcesDetails(resourceId);
    }


    @PostMapping("/search")
    public ApiResult search(@RequestBody SearchDto dto) {
        return resourceService.search(dto);
    }

//    @GetMapping ("/search")
//    public ApiResult searchTo(String search) {
//        return resourceService.search(search);
//    }

    @GetMapping("/getBookDataType/{dataTypeId}")
    public ApiResult getBookDataType(@PathVariable("dataTypeId") String dataTypeId) {
        if (dataTypeId == null) {
            return ApiResult.failed("数据异常，请联系管理员");
        }
        LambdaQueryWrapper<ResourceEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(ResourceEntity::getDataTypeId, dataTypeId);
        wrapper.ne(ResourceEntity::getCover, "http://61.163.243.7:9999/digitallibrary/file/lengleng/暂无封面.jpg");
        wrapper.orderByDesc(ResourceEntity::getCreateTime);
        return ApiResult.ok(resourceService.list(wrapper));
    }

    @GetMapping("/getBookDataType/getPageView/{dataTypeId}")
    public ApiResult getBookDataTypePageView(@PathVariable("dataTypeId") String dataTypeId) {
        if (dataTypeId == null) {
            return ApiResult.failed("数据异常，请联系管理员");
        }
        LambdaQueryWrapper<ResourceEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(ResourceEntity::getDataTypeId, dataTypeId);
        wrapper.orderByDesc(ResourceEntity::getPageView);
        return ApiResult.ok(resourceService.list(wrapper));
    }

    @PostMapping("/getBookType")
    public ApiResult getBookType(ResourceEntity resource) {
        if (resource.getDataTypeId() == null) {
            return ApiResult.failed("数据异常，请联系管理员");
        }
        if (resource.getBookTypeId() == null) {
            return ApiResult.failed("数据异常，请联系管理员");
        }
        LambdaQueryWrapper<ResourceEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(ResourceEntity::getDataTypeId, resource.getDataTypeId());
        wrapper.eq(ResourceEntity::getBookTypeId, resource.getBookTypeId());
        wrapper.orderByDesc(ResourceEntity::getCreateTime);
        return ApiResult.ok(resourceService.list(wrapper));
    }

    @PostMapping("/getBookType/getPageView")
    public ApiResult getBookTypePageView(ResourceEntity resource) {
        if (resource.getDataTypeId() == null) {
            return ApiResult.failed("数据异常，请联系管理员");
        }
        if (resource.getBookTypeId() == null) {
            return ApiResult.failed("数据异常，请联系管理员");
        }
        LambdaQueryWrapper<ResourceEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(ResourceEntity::getDataTypeId, resource.getDataTypeId());
        wrapper.eq(ResourceEntity::getBookTypeId, resource.getBookTypeId());
        wrapper.orderByDesc(ResourceEntity::getPageView);
        return ApiResult.ok(resourceService.list(wrapper));
    }

    @GetMapping("/books/{id}")
    public ApiResult books(@PathVariable("id") Long id) {
        return resourceService.books(id);
    }

    @GetMapping("/video/{id}")

    public ApiResult video(@PathVariable("id") Long id) {
        return resourceService.video(id);
    }

    @GetMapping("/audio/{id}")

    public ApiResult audio(@PathVariable("id") Long id) {
        return resourceService.audio(id);
    }

    @GetMapping("/periodical/{id}")

    public ApiResult periodical(@PathVariable("id") Long id) {
        return resourceService.periodical(id);
    }


    @GetMapping(value = "/particularsBooks/{ids}")
    public ApiResult particularsBooks(@PathVariable("ids") String ids) {
        return resourceService.particularsBooks(ids);
    }


    @PostMapping("/getJournalSearchResult")
    public ApiResult getJournalSearchResult(String searchKeywords,String openId) {
        return resourceService.getJournalSearchResult(searchKeywords,openId);
    }

    @PostMapping("/getJournalSearchResultWXMini")
    public ApiResult getJournalSearchResultWXMini(@RequestBody Map<String, String> map) {
        String s = map.get("searchKeywords");
        return resourceService.getJournalSearchResult(s,"");
    }

    @PostMapping("/getArticleCatalog")
    public ApiResult getArticleCatalog(ArticleCatalogVo articleCatalogVo) {
        return resourceService.getArticleCatalog(articleCatalogVo);
    }

    @PostMapping("/getArticleCatalogWxMini")
    public ApiResult getArticleCatalogWxMini(@RequestBody ArticleCatalogVo articleCatalogVo) {
        return resourceService.getArticleCatalog(articleCatalogVo);
    }

    @GetMapping("/getArticleDetail/{id}")
    public ApiResult getArticleDetail(@PathVariable("id") String id) {
        return resourceService.getArticleDetail(id);
    }

    @GetMapping("like")
    @ApiOperation(value = "点赞",response = Boolean.class)
    public ApiResult<Boolean> like(Long id,String openId){
        return ApiResult.ok(resourceService.like(id,openId));
    }

    @GetMapping("cancelLike")
    @ApiOperation(value = "取消点赞",response = Boolean.class)
    public ApiResult<Boolean> cancelLike(Long id,String openId){
        return ApiResult.ok(resourceService.cancelLike(id,openId));
    }
}
